package com.atguigu.ggkt.vod.service.impl;


import com.atguigu.ggkt.vod.mapper.VideoVisitorMapper;
import com.atguigu.ggkt.vod.service.VideoVisitorService;
import com.atguigu.ggkt.model.vod.VideoVisitor;
import com.atguigu.ggkt.vo.vod.VideoVisitorCountVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 视频来访者记录表 服务实现类
 * </p>
 *
 * @author sleephardzz
 * @since 2023-07-19
 */
@Service
public class VideoVisitorServiceImpl extends ServiceImpl<VideoVisitorMapper, VideoVisitor> implements VideoVisitorService {

    @Override
    public Map<String, Object> findCount(Long courseId, String startDate, String endDate) {
        /**     <select id="findCount" resultType="com.atguigu.ggkt.vo.vod.VideoVisitorCountVo">
         SELECT
         DATE(join_time) AS joinTime,
         COUNT(*) AS userCount
         FROM video_visitor
         <where>
         <if test="startDate != null and startDate != ''">
         AND DATE(join_time) >= #{startDate}
         </if>
         <if test="endDate != null and endDate != ''">
         AND DATE(join_time) &lt;= #{endDate}
         </if>
         and course_id=#{courseId}
         </where>
         GROUP BY DATE(join_time)
         ORDER BY DATE(join_time)
         </select> */
        List<VideoVisitorCountVo> videoVisitorCountVoList =  baseMapper.findCount(courseId,startDate,endDate);
        HashMap<String, Object> map = new HashMap<>();
//        ArrayList<Integer> countList = new ArrayList<>();
        //封装数据：流的方式
        List<String> dateList = videoVisitorCountVoList.stream().map(VideoVisitorCountVo::getJoinTime)
                .collect(Collectors.toList());
        List<Integer> countList = videoVisitorCountVoList.stream().map(VideoVisitorCountVo::getUserCount)
                .collect(Collectors.toList());
        map.put("xData",dateList);//x轴
        map.put("yData", countList);//y轴
        return map;
    }
}
